<?php
namespace Message\Controller;
use Common\Controller\MessageController;
class BraintwisterController extends MessageController {


    /**
     * 缓存到文件
     */
    private function cachebraint($appid){

        $list = S('cachebraint');

        if (!empty($list)){
            return $list;
        }


        $where2['appid'] = $appid;
        $where2['status'] = '-1';

        $info = M('wxopen_template_send')->field('wxopen_template_send_id,appid,touser,template_id,page,form_id')->where($where2)->order('wxopen_template_send_id asc')->select();

        $data = array();
        foreach ($info as $key=>$value){

            $data[$value['touser']] = $value;

        }

        S('cachebraint',$data,7200);
        $list = S('cachebraint');
        return $list;

    }

    private function cacherank($appid){


        $cacherank =  S('taskcountrank');

        if (!empty($cacherank)){
            return $cacherank;
        }

        $year = date('Y');
        $week = (int)date('W');

        $where = array(
            'week.year'=>$year,
            'week.challenge_week'=>$week,
            'week.appid'=>$appid,
        );

        $list =M()->table('mj_challenge_week as week')->join('mj_wxusers as uesr on  week.wxusers_id = uesr.wxusers_id')->where($where)->field('week.wxusers_id,uesr.nickName,uesr.avatarUrl')->limit(200)->order('week.passnum desc' )->select();

        S('taskbraintrank',$list,7200);
        $cacherank =  S('taskbraintrank');

        return $cacherank;

    }

    /**
     * 脑筋
     * @return int
     */
    public function sendnaojin2(){

        exit;

        $appid = "wxbcb264959c7f8ffe";  //脑力王者

//        $where2['appid'] = $appid;
//        $where2['status'] = '-1';
//        $where2['touser'] = "o540M5P7qhJalWCo5q4yqd6UdWWo";
//
//
//        $info = M('wxopen_template_send')->field('wxopen_template_send_id,appid,touser,template_id,page,form_id')->where($where2)->limit(1)->order('wxopen_template_send_id desc')->select();



        $secret = "e5400b4ebeba664cb0427e8f05cd8259";

        $template_id = "wTeNkiUUvlu3Xz3kKrGEhmrQP4cltjq-BdimpDUanyE";

       $info = $this->cachebraint($appid);


        foreach ($info as $key=>$value){

            $fdata = array(
                'keyword1'=>array(
                    'value'=>'脑筋大转弯',
                    'color'=>'#173177',
                ),
                'keyword2'=>array(
                    'value'=>'开卷',
                    'color'=>'#173177',
                ),
                'keyword3'=>array(
                    'value'=>'新增众多趣味题目，欢迎参加考试！',
                    'color'=>'#ff0000',
                ),

            );

            $data = array(
                'touser'=>$value['touser'],
                'template_id'=>$template_id,
                'page'=>'pages/index/index',
                'form_id'=>$value['form_id'],
                'data'=>$fdata,
            );
            $res = $this->sendtemplate($appid,$secret,$data);
            $save = array(
                'errcode'=>$res['errcode'],
                'errmsg'=>$res['errmsg'],
                'data'=>json_encode($data['data']),
            );
            echo $res['errcode'];

            if ($res['errcode']==0){
                $save['status'] = 1;
                M('wxopen_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }else{
                $save['status'] = 2;
                M('wxopen_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }

        }

    }


    /**
     * 脑筋
     * @return int
     */
    public function sendnaojin(){

        exit;
//        $appid = "wxbcb264959c7f8ffe";
//
//        $where2['appid'] = $appid;
//        $where2['status'] = '-1';
//        $where2['touser'] = "o5SjM4qHGZ7s31KForGK2z3dTgQw";
//
//
//        $info = M('wxopen_template_send')->field('wxopen_template_send_id,appid,touser,template_id,page,form_id')->where($where2)->limit(1)->order('wxopen_template_send_id desc')->select();
//
       $appid = "wxbcb264959c7f8ffe";  //脑力王者

        $info = $this->cachebraint();

        $list =$this->cacherank();
        $first = $list[0]['nickname'];
        $myrank = 0;

        foreach ($info as $key=>$value){

            $member_id = M('wxusers')->where(array('openId'=>$value['touser']))->getField('wxusers_id');

            foreach ($list as $lkey=>$lval){

                if ($list[$key]['wxusers_id']==$member_id){
                    $myrank = $key+1;
                }

            }
            $myrank = $myrank.'名';
            if ($myrank==0){
                $myrank = '未上榜';
            }
            $fdata = array(
                'keyword1'=>array(
                    'value'=>$first,
                    'color'=>'#173177',
                ),
                'keyword2'=>array(
                    'value'=>$myrank,
                    'color'=>'#173177',
                ),
                'keyword3'=>array(
                    'value'=>'好友排名超越您！',
                    'color'=>'#ff0000',
                ),
                'keyword4'=>array(
                    'value'=>'更多好玩游戏已上线，点击 “更多好玩”查看！！！',
                    'color'=>'#173177',
                ),
            );
            $template_id = "HPtDW7Xe9sje_F6-_bS_v6t5FeV1iw78baB6NkDdfww";
            $data = array(
                'touser'=>$value['touser'],
                'template_id'=>$template_id,
                'page'=>'pages/index/index',
                'form_id'=>$value['form_id'],
                'data'=>$fdata,
                'emphasis_keyword'=>'keyword3.DATA'
            );
            $res = $this->sendtemplate($appid,'e5400b4ebeba664cb0427e8f05cd8259',$data);
            $save = array(
                'errcode'=>$res['errcode'],
                'errmsg'=>$res['errmsg'],
                'data'=>json_encode($data['data']),
                'emphasis_keyword'=>$data['emphasis_keyword'],
            );
            echo $res['errcode'];

            if ($res['errcode']==0){
                $save['status'] = 1;
                M('wxopen_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }else{
                $save['status'] = 2;
                M('wxopen_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }

        }

    }


    /**
     * 脑经大转弯
     * @return int
     */
    public function sendnaojint(){

        exit;

//        $appid = "wxbcb264959c7f8ffe";
//
//        $where2['appid'] = $appid;
//        $where2['status'] = '-1';
//        $where2['touser'] = "o5SjM4qHGZ7s31KForGK2z3dTgQw";
//
//
//        $info = M('wxopen_template_send')->field('wxopen_template_send_id,appid,touser,template_id,page,form_id')->where($where2)->limit(1)->order('wxopen_template_send_id desc')->select();
//
        //$appid = "wxbcb264959c7f8ffe";  //脑力王者
        $appid = "wx2faae9d47390a9e7"; //脑经大转弯
        $info = $this->cachebraint();

        $list =$this->cacherank();
        $first = $list[0]['nickname'];
        $myrank = 0;

        foreach ($info as $key=>$value){

            $member_id = M('wxusers')->where(array('openId'=>$value['touser']))->getField('wxusers_id');

            foreach ($list as $lkey=>$lval){

                if ($list[$key]['wxusers_id']==$member_id){
                    $myrank = $key+1;
                }

            }
            $myrank = $myrank.'名';
            if ($myrank==0){
                $myrank = '未上榜';
            }
            $fdata = array(
                'keyword1'=>array(
                    'value'=>'好友排名超越您',
                    'color'=>'#173177',
                ),
                'keyword2'=>array(
                    'value'=>$myrank,
                    'color'=>'#ff0000',
                ),
            );
            $template_id = "0DaR15cOCc59QJicwwCqkUvK2lCB3bcIMsfpCoqNA48";
            $data = array(
                'touser'=>$value['touser'],
                'template_id'=>$template_id,
                'page'=>'pages/index/index',
                'form_id'=>$value['form_id'],
                'data'=>$fdata,
                'emphasis_keyword'=>'keyword2.DATA'
            );
            $res = $this->sendtemplate($appid,'ef9b7ee6ca62bc4aaf856a5dd8420388',$data);
            $save = array(
                'errcode'=>$res['errcode'],
                'errmsg'=>$res['errmsg'],
                'data'=>json_encode($data['data']),
                'emphasis_keyword'=>$data['emphasis_keyword'],
            );
            echo $res['errcode'];

            if ($res['errcode']==0){
                $save['status'] = 1;
                M('wxopen_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }else{
                $save['status'] = 2;
                M('wxopen_template_send')->where(array('wxopen_template_send_id'=>$value['wxopen_template_send_id']))->save($save);
            }

        }

    }



}